package com.ruoyi.work.service.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.exception.ServiceException;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.work.domain.WorkProject;
import com.ruoyi.work.mapper.WorkProjectMapper;
import com.ruoyi.work.service.IWorkProjectService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Author: MMDZ
 * @Desc:
 */
@Service
public class WorkProjectServiceImpl implements IWorkProjectService {

    @Autowired
    private WorkProjectMapper workProjectMapper;

    /**
     * 查询所有项目配置列表 - 条件 分页
     *
     * @param workProject
     * @return
     */
    @Override
    public List<WorkProject> selectWorkProjectList(WorkProject workProject) {
        return workProjectMapper.selectWorkProjectList(workProject);
    }

    /**
     * 根据id查询项目配置详情
     *
     * @param projectId
     * @return
     */
    @Override
    public WorkProject selectWorkProjectById(Long projectId) {
        return workProjectMapper.selectWorkProjectById(projectId);
    }

    /**
     * 项目代码是否重复
     *
     * @param workProject
     * @return
     */
    @Override
    public String checkProjectCodeUnique(WorkProject workProject) {
        Long projectId = StringUtils.isNull(workProject.getProjectId()) ? -1L : workProject.getProjectId();
        WorkProject info = workProjectMapper.checkProjectCodeUnique(workProject.getProjectCode());
        if (StringUtils.isNotNull(info) && info.getProjectId().longValue() != projectId.longValue()) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询所有项目配置列表
     *
     * @return
     */
    @Override
    public List<WorkProject> selectProjectAll() {
        return workProjectMapper.selectProjectAll();
    }

    /**
     * 新增
     *
     * @param workProject
     * @return
     */
    @Override
    public int insertWorkProject(WorkProject workProject) {
        return workProjectMapper.insertWorkProject(workProject);
    }

    /**
     * 修改
     *
     * @param workProject
     * @return
     */
    @Override
    public int updateWorkProject(WorkProject workProject) {
        return workProjectMapper.updateWorkProject(workProject);
    }

    /**
     * 删除
     *
     * @param projectIds
     * @return
     */
    @Override
    public int deleteWorkProjectByIds(Long[] projectIds) {
        for (Long projectId : projectIds) {
            WorkProject project = selectWorkProjectById(projectId);
            if (countProjectFillById(projectId) > 0) {
                throw new ServiceException(String.format("%1$s项目已分配,不能删除", project.getProjectCode()));
            }
        }
        return workProjectMapper.deleteWorkProjectByIds(projectIds);
    }

    /**
     * 通过项目ID查询填报使用数量
     *
     * @param projectId 项目ID
     * @return 结果
     */
    @Override
    public int countProjectFillById(Long projectId) {
        return workProjectMapper.countProjectFillById(projectId);
    }

}
